//
//  CrazyChatMineVoiceCell.m
//  webSocket
//
//  Created by 马军凯 on 2019/2/15.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "CrazyChatMineVoiceCell.h"

@interface CrazyChatMineVoiceCell ()

@property (nonatomic, strong) UIImageView *mineHeadImageView; //头像
@property (nonatomic, strong) UIView *mineVoiceBackView; //背景
@property (nonatomic, strong) UIImageView *mineBackImageView; //背景
@property (nonatomic, strong) UILabel *mineVoiceTimeLabel; //时间
//语音播放的图片
@property (nonatomic, strong) UIImageView *mineVoiceImageView;

@end

@implementation CrazyChatMineVoiceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifier = CrazyChatMineVoiceIdentifier;
    
    //缓存中取
    CrazyChatMineVoiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //创建
    if (!cell) {
        cell = [[CrazyChatMineVoiceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        //头像
        self.mineHeadImageView = [[UIImageView alloc] init];
        self.mineHeadImageView.frame = CGRectMake(CRAZY_SCREEN_WIDTH - 55, 10, 40, 40);
        [self.contentView addSubview:self.mineHeadImageView];
        //背景
        self.mineVoiceBackView = [[UIView alloc] init];
        self.mineVoiceBackView.frame = CGRectMake(60, 10, CRAZY_SCREEN_WIDTH - 120, 40);
        [self.contentView addSubview:self.mineVoiceBackView];
        //背景图片
        self.mineBackImageView = [[UIImageView alloc] init];
        self.mineBackImageView.image = [[UIImage imageNamed:@"Crazy_chat_rightPic@2x.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(23, 15, 7, 15) resizingMode:UIImageResizingModeStretch];
        [self.mineVoiceBackView addSubview:self.mineBackImageView];
        //语音标识
        self.mineVoiceImageView = [[UIImageView alloc] init];
        self.mineVoiceImageView.image = [UIImage imageNamed:@"CrazyChat_mine_voice.png"];
        [self.mineVoiceBackView addSubview:self.mineVoiceImageView];
        //时间
        self.mineVoiceTimeLabel = [[UILabel alloc] init];
        self.mineVoiceTimeLabel.font = [UIFont systemFontOfSize:14];
        self.mineVoiceTimeLabel.textColor = CRAZY_RGBR(33);
        [self.mineVoiceBackView addSubview:self.mineVoiceTimeLabel];
        //播放按钮
        self.mineVoicePlayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.mineVoiceBackView addSubview:self.mineVoicePlayBtn];
        //重发按钮
        self.mineVoiceRepeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.mineVoiceRepeatBtn setImage:[UIImage imageNamed:@"Crazy_chat_repeat.png"] forState:UIControlStateNormal];
        self.mineVoiceRepeatBtn.size = CGSizeMake(30, 30);
        self.mineVoiceRepeatBtn.y = 10 + 5;
        [self.contentView addSubview:self.mineVoiceRepeatBtn];
    }
    return self;
}



//赋值
- (void)setCellValueWithHeadURL:(NSString *)headURL timeString:(NSString *)timeString repeatType:(NSString *)repeatType isPlay:(BOOL)isPlay {
    //头像
    [self.mineHeadImageView sd_setImageWithURL:[NSURL URLWithString:headURL] placeholderImage:CRAZY_DEFAULT_HEADIMAGE];
    //时间
    self.mineVoiceTimeLabel.text = [NSString stringWithFormat:@"%@%@", timeString, @"\""];
    //时间的位置
    [self.mineVoiceTimeLabel sizeToFit];
    self.mineVoiceTimeLabel.x = 8;
    self.mineVoiceTimeLabel.y = 10;
    self.mineVoiceTimeLabel.height = 20;
    //总宽度
    CGFloat allWidth = CRAZY_SCREEN_WIDTH - 120;
    if ([timeString intValue] > 20) {
        self.mineVoiceBackView.width = allWidth;
        self.mineVoiceBackView.x = 60;
    } else if ([timeString intValue] < 5) {
        self.mineVoiceBackView.width = allWidth / 3;
        self.mineVoiceBackView.x = 60 + allWidth / 3 * 2;
    } else {
        CGFloat width = (([timeString intValue] - 5) * allWidth) / 20 + allWidth / 3;
        self.mineVoiceBackView.width = width;
        self.mineVoiceBackView.x = 60 + allWidth - width;
    }
    self.mineBackImageView.frame = CGRectMake(0, 0, self.mineVoiceBackView.width, 40);
    self.mineVoiceImageView.frame = CGRectMake(self.mineVoiceBackView.width - 30, 10, 20, 20);
    self.mineVoicePlayBtn.frame = self.mineVoiceBackView.bounds;
    //重发按钮
    self.mineVoiceRepeatBtn.x = 20 + self.mineVoiceBackView.x - 60;
    //重发按钮
    if ([repeatType isEqualToString:@"1"]) {
        self.mineVoiceRepeatBtn.hidden = NO;
    } else {
        self.mineVoiceRepeatBtn.hidden = YES;
    }
    //播放按钮
    if (isPlay) {
        self.mineVoiceImageView.image = [UIImage imageWithGifNamed:@"CrazyChat_mine_voicePlay"];
    } else {
        self.mineVoiceImageView.image = [UIImage imageNamed:@"CrazyChat_mine_voice.png"];
    }
}






- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
